//
//  DataOfModel.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/30.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "DataOfModel.h"

@implementation DataOfModel
+(DataOfModel *)shareInstance
{
    // 定义静态指针变量 保证指针只有一个 静态区的值程序结束时释放 如果不是静态的就会每次创建 仅初始化一次 单例对象不管是ARC还是MRC都不需要release
    static DataOfModel *dbManager = nil;
    //判断是否是第一次调用该方法
    if (!dbManager) {
        // 如果指针为空创建对象
        dbManager = [[DataOfModel alloc] init];
    }
    
    return dbManager;
}
/// 打开数据库
- (void)openDB
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"modelOfMintHome.db"];
    // 判断dbPoint指针中是否存在数据库 如果不存在 再创建
    // 参数1 : 数据库路径
    // 参数2 : 数据库指针地址
    int result = sqlite3_open(path.UTF8String, &dbPoint);
    NSLog(@"打开数据库 %d 路径 : %@", result, path);
    
}
/// 关闭数据库
- (void)closeDB
{
    sqlite3_close(dbPoint);
}

/// 创建表

-(void)createTable
{
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS model (cover_image_url text, title text, likes_count NSInteger, PRIMARY KEY AUTOINCREMENT)";
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"创建表"];
}

// 增加用户

-(void)insertUser:(modelOfMintHome *)model
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO model (cover_image_url,title,likes_count) values ('%@', '%@', '%ld')", model.cover_image_url, model.title, model.likes_count];
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"添加用户"];
}

// 查询

-(NSMutableArray *)select
{
    NSString *selectedSQL = @"SELECT * FROM model";
    //创建数据库替身
    sqlite3_stmt *stmt = nil;
    // 准备SQL语句 // -1 表示不限制
    // sqlite3_prepare_v2函数 把数据库对象/SQL语句/数据库替身 关联在一起
    int result = sqlite3_prepare(dbPoint, selectedSQL.UTF8String, -1, &stmt, nil);
    // 初始化准备的数组(不写在if里面是因为无法返回 或者要在else里也返回)
    NSMutableArray *arr = [NSMutableArray array];
    // 判断准备是否成功
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        // 遍历每一行数据
        // sqlite3_step(stmt) 获取每一行的信息
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 逐行获取每一列信息
            // 列数从0 开始
            const unsigned char *imgurl = sqlite3_column_text(stmt, 0);
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            int likecounts = sqlite3_column_int(stmt, 2);
            
            //创建modal对象
            modelOfMintHome *m = [modelOfMintHome new];
            m.cover_image_url = [NSString stringWithUTF8String:(const char *) imgurl];
            m.title = [NSString stringWithUTF8String:(const char *) title];
            m.likes_count = likecounts;
            //添加到数组
            [arr addObject:m];
        }
    }else
    {
        NSLog(@"查询失败%d", result);
    }
    return arr;
}
//更新数据库
- (void)updateUserWith:(modelOfMintHome *)newModel withId:(NSInteger)likecounts
{
    NSString *updateSQL = [NSString stringWithFormat:@"UPDATE model SET imgUrl = '%@', title = '%@',  WHERE likecounts = '%ld' ",newModel.cover_image_url,newModel.title,newModel.likes_count];
    int result = sqlite3_exec(dbPoint, updateSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"更新数据"];
}

///判断方法
- (void)judgeResult:(int)result message:(NSString *)message
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", message);
    }else{
        NSLog(@"%@失败, %d", message, result);
    }
}

@end
